/**
 * 
 */
package com.java.test.messagecenter.mapper;

import com.java.test.messagecenter.dto.HorseNoticeInfo;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 *
 */
@Component
public class HorseNoticeMapper {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selecthorsenoticeCount(HorseNoticeInfo info) {

		String sql = "select count(*) from message_horsenotice where 1=1 ";

		if (info.getStartTime() != null) {
			sql += " and TO_DAYS(start_time)= TO_DAYS('" + info.getStartTime() + "')";
		}

		int i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i;
	}

	public List<HorseNoticeInfo> selecthorsenoticeList(HorseNoticeInfo info, PageInfo<HorseNoticeInfo> page) {

		String sql = "select * from message_horsenotice where 1=1 ";

		if (info.getStartTime() != null) {
			sql += " and TO_DAYS(start_time)= TO_DAYS('" + info.getStartTime() + "')";
		}

		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by start_time desc limit ?,?";

		List<HorseNoticeInfo> list = jdbcTemplate.query(sql, new HorseNoticeInfo(), start, limit);

		return list;
	}

	public HorseNoticeInfo selecthorsenoticeInfo(HorseNoticeInfo info) {
		String sql = "select * from message_horsenotice where id=?";

		HorseNoticeInfo infos = jdbcTemplate.queryForObject(sql, new HorseNoticeInfo(), info.getId());

		return infos;
	}

	public int deletehorsenoticeInfo(long[] ids) {
		String sql = "delete from  message_horsenotice where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setLong(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	/**
	 * 跑马灯推送查询
	 * @author
	 * @date 2020/7/20 0020 9:54 
	 * @return java.util.List<com.java.test.messagecenter.dto.HorseNoticeInfo>
	 */
	public List<HorseNoticeInfo> selectHorseRaceLampPushList(String ids, LocalDateTime now) {

		String sql = "SELECT t.* FROM message_horsenotice t " +
				"WHERE t.end_time >= ? AND t.push_time <= ? " +
				"AND t.id not in("+ids+")";

		List<HorseNoticeInfo> list = jdbcTemplate.query(sql, new HorseNoticeInfo(),now,now);
		return list;
	}

	public int inserthorsenoticeInfo(HorseNoticeInfo info) {

		String sql = "insert into message_horsenotice(power_type,start_time,end_time,content,status)values(?,?,?,?,1)";

		int i = jdbcTemplate.update(sql, info.getPowerType(), info.getStartTime(), info.getEndTime(),
				info.getContent());
		return i;
	}

	public int updatehorsenoticeInfo(HorseNoticeInfo info) {

		String sql = "update message_horsenotice set power_type=?,start_time=?,end_time=?,"
				+ "content=? where id = ?";

		int i = jdbcTemplate.update(sql, info.getPowerType(), info.getStartTime(), info.getEndTime(),
				info.getContent(), info.getId());
		return i;
	}

}
